package luzhin.frames;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JFileChooser;

import luzhin.StandaloneLuzhin;


/**
 * Dialog controller.
 * After constructing, call setInitDialog(InitDialog). 
 * @author lacungus 
 *
 */
public class InitDialogController implements ActionListener {
	private InitDialog initDialog;
	private int whiteStrategyId;
	private int blackStrategyId;
	
	public InitDialogController() {
	}

	/**
	 * 
	 * @return ��������� �����.
	 */
	public int getWhiteStrategyId() {
		return whiteStrategyId;
	}
	
	/**
	 * @return ��������� ������.
	 */
	public int getBlackStrategyId() {
		return blackStrategyId;
	}
	
	/**
	 * 
	 * @return ���� � �����, �� �������� ����� �����������.
	 */
	public String getGameToLoadString() {
		if (initDialog.gameToLoadField.getText().equals("none"))
			return null;
		return initDialog.gameToLoadField.getText();
	}
	
	/**
	 * 
	 * @param initDialog
	 */
	public void setInitDialog(InitDialog initDialog) {
		this.initDialog = initDialog;
		initDialog.startButton.addActionListener(this);
		initDialog.loadButton.addActionListener(this);		
	}
	
	/**
	 * ��������� ������� "Start!" � "Load button".
	 */
	public void actionPerformed(ActionEvent ae) {
		if (ae.getSource().equals(initDialog.startButton)) {
			whiteStrategyId = 0;
			switch (initDialog.whiteStrategyBox.getSelectedIndex()) {
			case InitDialog.MANUAL_STRATEGY_ID : {
				whiteStrategyId = StandaloneLuzhin.MANUAL;
				break;
			}
			case InitDialog.AI_STRATEGY_ID : {
				whiteStrategyId = StandaloneLuzhin.BACKTRACK;
				break;
			}			
			}
	
			blackStrategyId = 0;
			switch (initDialog.blackStrategyBox.getSelectedIndex()) {
			case InitDialog.MANUAL_STRATEGY_ID : {
				blackStrategyId = StandaloneLuzhin.MANUAL;
				break;
			}
			case InitDialog.AI_STRATEGY_ID : {
				blackStrategyId = StandaloneLuzhin.BACKTRACK;
				break;
			}			
			}
			
			initDialog.dispose();
			return;
		} 
		if (ae.getSource().equals(initDialog.loadButton)) {
			JFileChooser fc = new JFileChooser();
			fc.setCurrentDirectory(new File("./saves"));
	        int returnVal = fc.showSaveDialog(initDialog);

	        if (returnVal == JFileChooser.APPROVE_OPTION) {
	            File file = fc.getSelectedFile();
	            initDialog.gameToLoadField.setText(file.getAbsolutePath());
	        } else {
	        }
		}
	}
}
